<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="模块名称" prop="modelName">
          <el-input v-model="form.modelName" size="small" placeholder="请输入模块名称"/>
        </el-form-item>
        <el-form-item label="商品来源" prop="productSource">
          <el-radio-group v-model="form.productSource">
            <el-radio label="0">类目</el-radio>
            <el-radio label="1">按商品分组</el-radio>
            <el-radio label="2">全店铺</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品数量" prop="productCount">
          <el-input v-model="form.productCount" type="number" style="width: 400px" size="small"
                    placeholder="请输入商品数量">
            <template slot="prepend">可创建商品不少于</template>
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <el-form-item label="创建顺序" prop="createSort">
          <div class="flex">
            <div v-for="(item,index) in dict.type.info_module_create_order" :key="index">
              <el-select v-model="createSortData['order'+index]" placeholder="请选择" clearable
                         size="small" style="width: 150px">
                <el-option v-for="dict in dict.type.info_module_create_order" :key="dict.value" :label="dict.label"
                           :value="dict.value"/>
              </el-select>
              <span v-if="index != dict.type.info_module_create_order.length-1" class="ml10 mr10"> ＞ </span>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="出单商品" prop="productOut">
          <el-radio-group v-model="form.productOut">
            <el-radio label="0">出单优先于访客</el-radio>
            <el-radio label="1">访客优先于出单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="高访客商品" prop="productVisitor">
          <el-radio-group v-model="form.productVisitor">
            <el-radio label="0">出单优先于访客</el-radio>
            <el-radio label="1">访客优先于出单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="模块有限期" prop="modelValidity">
          <el-input v-model="form.modelValidity" type="number" style="width: 400px" size="small"
                    placeholder="请输入">
            <template slot="prepend">更换模块不少于</template>
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="更换模块要求" prop="modelReplace">
          <el-input v-model="form.modelReplace" type="number" style="width: 400px" size="small"
                    placeholder="请输入">
            <template slot="append">天内主商品和信息模块中商品均未出单</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {queryProjectById, saveInfoModule} from "@/api/autoOperation/infoModule";

export default {
  name: "infoModule",
  dicts: ['info_module_create_order'],
  props: {
    storeId: {
      type: String,
      default: ""
    },
    configId: {
      type: String,
      default: ""
    },
    userId: { // 绑定店铺的用户id
      type: String,
      default: ""
    },
    storeUserName: { //店铺账号 cn码
      type: String,
      default: ""
    },
    isUseShareCode: { // 是否使用分享码
      type: String,
      default: "0"
    }
  },
  data() {
    return {
      buttonLoading: false,//确定提交表单按钮loading
      title: "新增信息模块配置",
      timeRange: "",//测评时间区间
      open: true,
      createSortData: {},
      storeSortData: {},
      form: {
        isBrand: '0',//有无品牌
        allianceDiscount: '0',//联盟折扣
      },
      brandList: [],//品牌列表
      categoryList: [],//商品类目列表
      rules: {
        modelName: [{required: true, message: "模块名称不能为空", trigger: "blur"}],
        productSource: [{required: true, message: "请选择商品来源", trigger: "change"}],
        productCount: [
          {required: true, message: "商品数量不能为空", trigger: "blur"},
          {validator: this.$Validate.isPositiveInteger,}],
        createSort: [{required: false, message: "请选择创建顺序", trigger: "change"},],
        productOut: [{required: true, message: "请选择出单商品", trigger: "change"}],
        productVisitor: [{required: true, message: "请选择高访客商品", trigger: "change"}],
        modelValidity: [
          {required: true, message: "模块有限期不能为空", trigger: "blur"},
          {validator: this.$Validate.isPositiveInteger,}
        ],
        modelReplace: [
          {required: true, message: "更换模块要求", trigger: "blur"},
          {validator: this.$Validate.isPositiveInteger}],
      }
    }
  },
  created() {
    if (this.configId) {
      this.getConfigDetail()
    } else {
      this.reset()
    }
  },
  methods: {
    // 获取上架配置详情
    getConfigDetail() {
      this.title = "修改信息模块配置"
      queryProjectById({id: this.configId}).then(res => {
        let dateList = []
        let data = res.data
        dateList.push(data.pulishBeginTime)
        dateList.push(data.pulishEndTime)
        let sortList = data.createSort.split(',')
        for (let i = 0; i < sortList.length; i++) {
          this.createSortData['order' + i] = sortList[i]
        }
        for (let key in data) {
          if (data[key] != null) {
            data[key] = '' + data[key]
          }
        }
        this.form = data
        this.groundTime = dateList
      })
    },
    // 确定提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let sort = "";
          for (let key in this.createSortData) {
            sort += this.createSortData[key] + ','
          }
          if (this.isUseShareCode == '1') {
            this.form.id = undefined;
            this.form.storeId = this.storeId
            this.form.userId = this.userId
            this.form.storeUserName = this.storeUserName
          }
          this.form.createSort = sort.slice(0, sort.length - 1)
          saveInfoModule(this.form).then(res => {
            this.$message.success(res.msg)
            this.cancel()
          })
        }
      })
    },
    reset() {
      this.form = {
        storeId: this.storeId,
        modelName: undefined,
        productSource: '0',
        productCount: undefined,
        createSort: "",
        productOut: '0',
        productVisitor: '0',
        modelValidity: undefined,
        modelReplace: undefined,
        storeUserName: this.storeUserName,
        userId: this.userId,
      }
      this.resetForm("form");
    },
    // 取消
    cancel() {
      this.$emit('close', false)
    },
  }
}
</script>

<style scoped>

</style>
